package com.mark.web.common.service.Impl;

import com.mark.common.exception.Campuso2oException;
import com.mark.web.common.service.IRedisCacheService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Mark
 * @CreateDate: 2020/5/29 23:08
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service
public class RedisCacheServiceImpl implements IRedisCacheService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public <K, V> void hashPut(String key, Map<K, V> map) {
        if(StringUtils.isBlank(key)){
            throw new Campuso2oException("key不能为空！");
        }
        if(map == null || map.size() == 0){
            throw new Campuso2oException("map不能为空！");
        }
        redisTemplate.opsForHash().putAll(key, map);
    }

    @Override
    public <K,V> Map<K, V>  getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public <E> void listPush(String key, List<E> list) {
        if(StringUtils.isBlank(key)){
            throw new Campuso2oException("key不能为空！");
        }
        if(CollectionUtils.isEmpty(list)){
            throw new Campuso2oException("list不能为空！");
        }
        redisTemplate.opsForList().leftPush(key,list);
    }

    @Override
    public <E> List<E> getListRange(String key, long var1, long var2) {
        return redisTemplate.opsForList().range(key,var1,var2);
    }

}
